;
(function () {
  new Vue({
    el: '#report_resources',
    data: {
      quanxian: JSON.parse(localStorage.getItem('permissions')),
      pageCounts: [{
          'name': '10',
          'code': 10
        },
        {
          'name': '15',
          'code': 15
        },
        {
          'name': '20',
          'code': 20
        },
      ],
      currentPage: 1,
      totalPage: 0,
      totalCount: 0,
      limit: ['10'],

      projectList: [],
      selectedProId: '',
      selectedProName: '',
      projectIdSelect: ['#'],
			projectIds: [],

      keyword: '',
      billDate: '',
      rentStatus: [				
        {
          'name': '预定',
          'code': 'YD'
        },
        {
          'name': '在租',
          'code': 'IR'
        },
        {
          'name': '空置',
          'code': 'VA'
        },
        {
          'name': '即将履约',
          'code': 'IP'
        },
        {
          'name': '即将到期',
          'code': 'DU'
        },
				{
				  'name': '已到期',
				  'code': 'FN'
				},
        {
          'name': '自用',
          'code': 'ZY'
        },
      ],
      selectedRent: [],
      untilContracts: [],
			loadingFlag: true
    },
    filters: {
			rentState (item) {
				let res;
				// YD预订、IR在租、VA空置、IP即将履约、DU即将到期、ZY自用 FN 已到期
				switch (item){
					case 'YD':
					res = '预订'
						break;
					case 'IR':
					res = '在租'
						break;
					case 'VA':
					res = '空置'
						break;
					case 'IP':
					res = '即将履约'
						break;
					case 'DU':
					res = '即将到期'
						break;
					case 'FN':
					res = '已到期'
						break;
					case 'ZY':
					res = '自用'
						break;					
					default:
					res ='--'
						break;
				}
				return res
			},
      toZero: function (n) {
        return n < 10 ? '0' + n : '' + n
      },
      UTCdate: function (time) {
				if (!time) return '--';
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      formatNum: function (num) {
        return hlNumberUtil.formatNum(num)
      },
      nullundefinded(item) {
        if (!item) return '--';
        return item.join('、')
      }
    },
    computed: {

    },
    methods: {
			rentClick() {
				this.loadingFlag = true
				this.getData()
			},
      // 获取列表数据
      getData() {
        let data = {
          project_id: this.projectIdSelect[0] || '#',
          project_ids: this.projectIds.toString(),
          project_name: this.selectedProName || '#',
          keyword: this.keyword || '#',
          billing_date: this.billDate || '#',
          rent_state: this.selectedRent + '' || '#',
          page: this.currentPage,
          page_count: this.limit + '',
        }
        hlhttp.get({
          url: 'report/unit_contract',
          data: data,
          success: res => {
            this.totalPage = res.totalPage
            this.totalCount = res.totalCount
            this.untilContracts = res.datas
						this.loadingFlag = false
          }
        })
      },
      // 获取所有项目
      getProject() {
        hlhttp.get({
          url: 'user/projects',
          success: (res) => {						
            res = hlArrayUtil.removeRepeat(res, 'projectId')
						res.map(item => {
							this.projectIds.push(item.projectId)
						})
            res.unshift({
              projectName: '所有项目',
              projectId: '#'
            })
            this.projectList = hlArrayUtil.replaceData(res, [{
              key: 'projectName',
              rKey: 'name'
            }, {
              key: 'projectId',
              rKey: 'code'
            }]);
						this.getData()
          }
        })
      },
      // 选择某个项目
      getProjectItem(arr) {
        this.selectedProId = arr[0].code
        this.selectedProName = arr[0].name
      },
      // 查询
      search() {
        
				this.currentPage = 1
				this.loadingFlag = true
				this.getData()
      },
      // 重置
      reset() {
				this.loadingFlag = true
        this.projectIdSelect = ['#']
        this.selectedProName = ''
        this.keyword = ''
        this.billDate = ''
        this.selectedRent = []
        this.currentPage = 1
        this.getData()
      },
      resetTabbar() {
        this.selectedRent = []
        this.currentPage = 1
        this.getData()
      },
      // 导出
      excl() {
				hlhttp.get({
				  baseUrl: exclUrl,
				  url: 'resource/report_project_unit_rent_contract_info/list_export',
				  data: {
				    project_id: this.projectIdSelect[0] || '#',
				    project_ids: this.projectIds.toString(),
				    project_name: this.selectedProName || '#',
				    keyword: this.keyword || '#',
				    billing_date: this.billDate || '#',
				    rent_state: this.selectedRent + '' || '#',
				  },
				  success: (data) => {
				    var a = document.createElement('a');
				    a.href = data.excel_url;
				    $('body').append($(a))
				    a.click();
				    setTimeout(function() {
				      $(a).remove()
				    }, 100);
							
				  }
				})
      },
    },
    mounted() {
      this.getProject()      
    }
  })

}())